package com.swa99ylum.pjv.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class PJVUtils {
	
	/** <h1>拷贝 String数组</h1>
	<br>    
	 */
	public static String[] copy(String[] array) {
		String[] array2 = new String[array.length];
		System.arraycopy(array, 0, array2, 0, array.length);
		
		return array2;
		
	}
	
	/** <h1>向 String数组 追加 元素</h1>
	<br>    
	 */
	public static String[] append(String[] array, String ele) {
		String[] array2 = new String[array.length+1];
		System.arraycopy(array, 0, array2, 0, array.length);
		
		array2[array2.length-1] = ele;
		
		return array2;
	}
	
	/** <h1>使用 "."，将 nodePath 拼接为 字符串 并 返回</h1>
	<br>    
	 */
	public static String getNodePathStr(String[] array) {
		
		return StringUtils.join(array, ".");
	}
	
	
	/** <h1>根据 nodePath 去读取 obj 中的 对象属性</h1>
	<br> 
	<br> 
	<br> 
	 * @param obj : 当前对象
	 * @param nodePath : 目标属性的 属性路径。eg: ["$", "student[0]", "id"]
	 */
	@SuppressWarnings("unchecked")
	public static Object getNodeValue(Object obj, String[] nodePath) {
		
		Object obj2 = obj;
		for (String ele : nodePath) {
			
			Pattern pattern1 = Pattern.compile("([\\s\\S]*?)\\[(\\d+)\\]"); //匹配 @{...}
			Matcher matcher1 = pattern1.matcher(ele);
			
			if(matcher1.matches()) {
				if(matcher1.group(1).equals("$")) {
					
				}else {
					Map<String, Object> map = (Map<String, Object>) obj2;
					obj2 = map.get(matcher1.group(1));
				}
				
				
//				if(matcher1.group(1).equals("$")) {
//					
//				}else {
//					obj2 = map.get(matcher1.group(1));
//				}
				
				List<Object> list = (List<Object>) obj2;
				obj2 = list.get(Integer.parseInt(matcher1.group(2)));
			}else {
				if(ele.equals("$")) {
					
				}else {
					Map<String, Object> map = (Map<String, Object>) obj2;
					obj2 = map.get(ele);
				}

				
				
//				if(ele.equals("$")) {
//					
//				}else {
//					obj2 = map.get(ele);
//				}
			}
		}
		
		return obj2;
//			return obj;
//			
//			if(isArrayJSONNode(ele) == null) { //按 map 读取
//				
//			}else { //按 map 读取
//				
//			}
//		}
		
		
//		String aaa111 = "student[41a23]";
//
//		Pattern pattern1 = Pattern.compile("([\\s\\S]*?)\\[(\\d+)\\]"); //匹配 @{...}
//		Matcher matcher1 = pattern1.matcher(aaa111);
//		
//		object
	}
	
	
	public static void main(String[] args) {
//		 String s1[]={"1","2","3","4","5","6"};
//		  String s2[]=new String[6];
//		  System.arraycopy(s1,0,s2,0,4);
		
//		System.out.println("=======================");
		
		String[] asd = PJVUtils.append(new String[]{"asd", "asdasd", "xxx"},  "asds"); 
		System.out.println(Arrays.toString(asd));
	}
}
